/**
 * @FileName: LoginController.java
 * @Author
 * @Description:
 * @Date 2016年1月4日 上午11:39:53
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.controller.client;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hpclub.server.constant.ClientApiConstant;
import cn.hpclub.server.model.MemberOrg;
import cn.hpclub.server.restful.API;
import cn.hpclub.server.service.LoginService;
import cn.hpclub.server.service.LoginService.EmpCodeIsEmptyException;
import cn.hpclub.server.service.LoginService.VerifyEmpCode9999Exception;
import cn.hpclub.server.service.LoginService.VerifyEmpCodeException;

@API("/StoreServer/Store")
public class StoreController extends BaseClientController<MemberOrg>{
    private final static String CODE                = "signcode";
    private final static String ORIGIN              = "origin";
    private final static String ORIGIN_IOS          = "ios";
    private final static String ORIGIN_ANDROID      = "1";
    private final static String VERSION             = "v";
    private final static String IOS_VERSION_OLD     = "115";
    private final static String ANDROID_VERSION_OLD = "119";
    private final static String SPEC_TYPE           = "spectype";
    private final static int    VISITOR             = 1;

    private static Logger       log                 = LoggerFactory.getLogger(StoreController.class);

    public void VerifyAccount(){
        JSONObject jo = setResponseObj(ClientApiConstant.ResultCode.FAILED, "版本过低，请下载最新版本",
                                       ClientApiConstant.Msg.EMPTY);
        renderText(jo.toString());

        // JSONObject jo =
        // setResponseObj(ClientApiConstant.ResultCode.SERVER_EXCEPTION,
        // ClientApiConstant.Msg.EXCEPTION,
        // ClientApiConstant.Msg.EMPTY);
        // try{
        // String username = getPara(ClientApiConstant.Key.USERNAME);
        // String password = getPara(ClientApiConstant.Key.PASSWORD);
        // String desPwd = getPara(ClientApiConstant.Key.PTAG);
        // boolean isDesPwd = ("1".equals(desPwd)) ? true : false;
        // String smsCode = getPara(CODE);
        // String origin = getPara(ORIGIN);
        // String v = getPara(VERSION);
        // String visitType = getPara(SPEC_TYPE);
        //
        // // 校验手机号，是不是11位数字。主要是针对后面带空格的情况，提示错误。
        //
        // if(!Utils.isMobileNO(username)){
        // log.error("illegal username: '" + username + "'");
        // jo = setResponseObj(ClientApiConstant.ResultCode.PARAM_ERROR,
        // ClientApiConstant.Msg.ILLEGAL_USERNAME,
        // ClientApiConstant.Msg.EMPTY);
        // renderText(jo.toString());
        // return;
        // }
        //
        // // IOS低版本，无需smscode；逛一逛用户有spectype=1,smscode为88888
        // if(needSmsCode(origin, v)){
        // if((smsCode == null) || ("".equals(smsCode))){
        // log.error("smscode empty:" + username);
        // jo = setResponseObj(ClientApiConstant.ResultCode.PARAM_ERROR,
        // ClientApiConstant.Msg.SMS_CODE_EMPTY,
        // ClientApiConstant.Msg.EMPTY);
        // renderText(jo.toString());
        // return;
        // }
        // }
        //
        // LoginService loginService = new LoginService(username, password,
        // isDesPwd);
        // ResultCode result = loginService.validate();
        // if(result != ResultCode.SUCCESS){
        // log.error("validate failed.userName=" + username);
        // jo = setResponseObj(ClientApiConstant.ResultCode.NOT_EXIST,
        // ClientApiConstant.Msg.ACCOUNT_NOT_EXIST_OR_PASSWD_ERROR,
        // ClientApiConstant.Msg.EMPTY);
        // renderText(jo.toString());
        // return;
        // }
        //
        // if(needSmsCode(origin, v)){
        // JSONObject smsVerifyResult = null;
        // if(isVisitor(visitType)){
        // smsVerifyResult = SmsService.service.verifyConstantSmsCode(smsCode);
        // } else{
        // smsVerifyResult = SmsService.service.VerifySignCodeLogin(username,
        // smsCode);
        // }
        //
        // if(smsVerifyResult == null){
        // jo = setResponseObj(ClientApiConstant.ResultCode.FAILED,
        // ClientApiConstant.Msg.SMS_CODE_INVALIDATE_ERROR,
        // ClientApiConstant.Msg.EMPTY);
        // renderText(jo.toString());
        // log.error("verify smscode failed 1,exception:" + username);
        // return;
        // }
        //
        // if(smsVerifyResult.getJSONObject("ROOT").getLong("RETURN_CODE")!=0){
        // jo = setResponseObj(ClientApiConstant.ResultCode.FAILED,
        // smsVerifyResult.getJSONObject("ROOT").getString("USER_MSG"),
        // ClientApiConstant.Msg.EMPTY);
        // renderText(jo.toString());
        // log.error("verify smscode failed 2:" + username);
        // return;
        //
        // }
        // }
        //
        // String token = TokenService.service.getToken(username);
        // if(StrKit.isBlank(token)){
        // log.error("token is empty. userName=" + username);
        // jo = setResponseObj(ClientApiConstant.ResultCode.FAILED,
        // ClientApiConstant.Msg.EXCEPTION,
        // ClientApiConstant.Msg.EMPTY);
        // renderText(jo.toString());
        // return;
        // }
        // jo = getData(loginService, token);
        //
        // }
        // catch(Exception e){
        // e.printStackTrace();
        // String errMsg = e.getMessage();
        // if(errMsg == null){
        // errMsg = "异常信息为null";
        // }
        // log.error("exception:" + errMsg.length() + ":" + errMsg);
        // jo = setResponseObj(ClientApiConstant.ResultCode.SERVER_EXCEPTION,
        // "异常:" + (errMsg.length() > 50 ? errMsg.substring(0, 50) : errMsg),
        // ClientApiConstant.Msg.EMPTY);
        // }
        // renderText(jo.toString());
    }

    /**
     * @Title: isIosLowVersion
     * @Description:TODO
     * @param origin
     * @param v
     *            void
     */

    private boolean isIosOlderVersion(String origin, String v){
        // log.info("device type:" + origin + ",version:" + v);
        if((origin != null)
                && (ORIGIN_IOS.equalsIgnoreCase(origin) && (v != null) && (v.compareTo(IOS_VERSION_OLD) == 0))){
            // log.info("ios older version");
            return true;
        }
        return false;
    }

    private boolean isIosOlderApkVersion(String origin, String v){
        log.info("device type:" + origin + ",version:" + v);
        if((origin != null)
                && (ORIGIN_ANDROID.equalsIgnoreCase(origin) && (v != null) && (v.compareTo(ANDROID_VERSION_OLD) == 0))){
            log.info("android older version");
            return true;
        }
        return false;
    }

    private boolean isVisitor(String visitType){
        if((visitType == null) || ("".equals(visitType))){
            return false;
        }
        int type = Integer.valueOf(visitType);
        if(type == VISITOR){
            return true;
        }
        return false;

    }

    /**
     * @Title: specialClientOrUser
     * @Description:IOS 低版本,无需smsmcode。其他需要smscode
     * @param origin
     * @param v
     * @param
     * @return boolean
     */
    private boolean needSmsCode(String origin, String v){
        // if(isIosOlderVersion(origin, v)){
        // return false;
        // }
        //
        // if(isIosOlderApkVersion(origin, v)){
        // return false;
        // }

        return true;
    }

    /**
     * @Title: getData
     * @Description:获取客户端登录需要相关参数
     * @param loginService
     *            登录Service
     * @param token
     *            登录Token
     * @return JSONObject 返回Json对象
     */
    private JSONObject getData(LoginService loginService, String token){
        JSONObject jo;
        try{
            JSONObject data = loginService.getData(token);
            jo = setResponseObj(ClientApiConstant.ResultCode.SUCCESS_CODE, ClientApiConstant.Msg.SUCCESS, data);
        }
        catch(EmpCodeIsEmptyException e){
            e.printStackTrace();
            jo = setResponseObj(ClientApiConstant.ResultCode.FAILED, ClientApiConstant.Msg.EMP_CODE_ERROR,
                                ClientApiConstant.Msg.EMPTY);
        }
        catch(VerifyEmpCode9999Exception e){
            e.printStackTrace();
            jo = setResponseObj(ClientApiConstant.ResultCode.FAILED, e.getMessage(), ClientApiConstant.Msg.EMPTY);
        }
        catch(IOException | VerifyEmpCodeException | JSONException e){
            e.printStackTrace();
            jo = setResponseObj(ClientApiConstant.ResultCode.FAILED, ClientApiConstant.Msg.MSG_EXCEPTION,
                                ClientApiConstant.Msg.EMPTY);
        }
        catch(Exception e){
            e.printStackTrace();
            jo = setResponseObj(ClientApiConstant.ResultCode.FAILED, ClientApiConstant.Msg.MSG_EXCEPTION,
                                ClientApiConstant.Msg.EMPTY);
        }
        return jo;
    }
}
